import { createStore } from "vuex";
import http from "@/global/http";
import Storage from '@/global/storage';

export default createStore({
    state: {
        collapse: false,
        userInfo: {},
        tabList: Storage.get("tabs") || [{ title: '数据中心', path: '/home' }],
        allDelBtnVisible: false
    },
    mutations: {
        updateCollapseStatus(state, status) {
            if (status == null) {
                state.collapse = !state.collapse;
            } else {
                state.collapse = status;
            }
        },
        // 设置user
        SET_USER_INFO(state, data) {
            state.userInfo = data;
        },
        // 添加header的tags
        ADD_TABS(state, item) {
            let status = state.tabList.some(data => data.path === item.path);
            if (!status) {
                state.tabList.push({
                    title: item.name,
                    path: item.path
                });
            }

            Storage.set("tabs", state.tabList);
        },
        // 删除header的tags
        REMOVE_TABS(state, index) {
            if (state.tabList.length > 1) {
                state.tabList.splice(index, 1);
            }

            Storage.set("tabs", state.tabList);
        },
        updateAllDelBtnVisible(state, status) {
            state.allDelBtnVisible = status;
        }
    },
    actions: {
        // 获取用户信息
        async getUserInfo({ commit }) {
            const { code, data } = await http.get('/user/info');
            if (code === 200) {
                commit('SET_USER_INFO', data);
            }
        }
    },
    modules: {}
});
